package com.kd.sms.provider;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kd.sms.ISmsProvider;
import com.kd.sms.config.SmsProperties;
import com.kd.sms.event.SendSmsEvent;
import com.kd.sms.model.req.SendSmsReq;
import com.kd.sms.model.resp.ISmsResp;
import com.kd.sms.model.resp.SmsRespData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public abstract class AbstractProvider implements ISmsProvider {

    protected static Map<String, String> templateIdMap;

    protected final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    protected SmsProperties smsProperties;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public AbstractProvider() throws Exception {
        this.initialize();
    }

    protected abstract void initialize() throws Exception;

    protected abstract String resolveTemplateId(SendSmsReq req);

    protected String getDefSignName() {
        return "medilink";
    }

    protected void publishSendSmsEvent(ISmsResp<SmsRespData> resp) {
        this.applicationEventPublisher.publishEvent(new SendSmsEvent(this, resp));
    }
}
